// pages/mall/home/home.js
import * as API_Mall_Classify from '../../../api/mall/classify'
import * as API_Mall_Product from '../../../api/mall/product'
import * as API_Mall_Swiper from '../../../api/mall/swiper'
import * as API_Mall_Bind from '../../../api/mall/bind'
import * as util from '../../../utils/util'
// const app = getApp()
// const API_Mall_Classify = app.load('./api/mall/classify')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showSwiper:true,
    locationFail:false,
    tab_index:0,
    headshow:0,
    adList:[],
    firstClassifyList:[],
    secondClassifyList:[],
    productList:[],
    isFinshed:false,
    params:{
      firstClassifyId:'',
      secondClassifyId:-1,
      pageNum:1,
      pageSize:20,
      orderByColumn:'{"whetherSelling":"descending","salesNum":"descending"}',
    },
    searchValue:''
  },
  changeInput(e){
    this.setData({
      searchValue:e.detail.value
    })
  },
  search(){
    let that = this
    if(!this.data.searchValue){
      return
    }
    wx.navigateTo({
      url: '../proSearch/proSearch?search='+that.data.searchValue,
    })
  },
  loadCartNum(){
    let guide = this.selectComponent("#cart")
    guide.loadCount()
   },
  goProductDetail(e){
    wx.navigateTo({
      url: '../../mall/proDetail/proDetail?id='+e.currentTarget.dataset.id,
    })
  },
  scroll :function(e){
    let deltaX=e.detail.deltaX;
    let scrollWidth=e.detail.scrollWidth;
    let scrollLeft=e.detail.scrollLeft+80;
    let clientWidth=getApp().globalData.clientWidth;
    let rightX=scrollWidth-clientWidth;
    let opacity = 1
    if(rightX<scrollLeft){
      opacity = (100-(scrollLeft- rightX)*3)/100
    }
    this.setData({headshow:opacity})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    if(options.scene){
      const scene = decodeURIComponent(options.scene);
      let sceneJso=util.getUrlVars(scene);
      this.bindStore(sceneJso.bindId)
    }
   
    this.initGlobalData();
    this.getFirstList()
    this.loadAdList()
  },
  bindStore(e){
    API_Mall_Bind.bindStore(e)
  },
  loadAdList(){
    API_Mall_Swiper.getList().then(res=>{
      this.setData({adList:res})
    })
  },
  getFirstList(){
    API_Mall_Classify.getFirstList().then(res=>{
      let arr = [{
        classifyName: "热卖",
        id: 0,
        parentId:-1,
        recommend:'Y'
      }]
      arr = [...arr,...res]
      this.setData({firstClassifyList:arr})
      // this.getSecond(arr[0])
      this.getHot()
    })
  },
  getHot(){
    API_Mall_Classify.getHot().then(res=>{
      this.setData({
        secondClassifyList:res.data
      })
    })
    this.getProductHot()
  },
  getProductHot(){
    API_Mall_Product.getHot(this.data.params).then(res=>{
      let arr = this.data.productList
      arr.push(...res.rows)
      this.setData({
        productList:arr,
        isFinshed:res.lastPage
      })
    })
  },
   getProductList(){
    API_Mall_Product.getList(this.data.params).then(res=>{
      let arr = this.data.productList
      arr.push(...res.rows)
      this.setData({
        productList:arr,
        isFinshed:res.lastPage
      })
    })
  },
  getClassifyProduct(e){
    API_Mall_Product.getHot().then(res=>{
      this.setData({
        productList:res.data,
        isFinshed:false
      })
    })
  },
  getSecond(e){
    // secondClassifyList
    API_Mall_Classify.getSecond(e).then(res=>{
      this.setData({
        secondClassifyList:res.data
      })
    })
  },
  
  switchTab(e){
    
    let tab_index=e.currentTarget.dataset.tab_index;
    this.setData({
      tab_index:tab_index,
      isFinshed:false,
      productList:[],
      'params.firstClassifyId':e.currentTarget.dataset.parentId,
      'params.secondClassifyId':-1,
      'params.pageNum':1,
      'params.pageSize':20
    })
    if(tab_index ==0){
      this.getHot()
      this.setData({showSwiper:true});
    }else{
      this.setData({showSwiper:false});
      this.getSecond(e.currentTarget.dataset.parentId)
      this.getProductList()
    }
  },
  switchSecondTab(e){
    // this.setData({
    //   isFinshed:false,
    //   productList:[],
    //   'params.firstClassifyId':-1,
    //   'params.secondClassifyId':e.currentTarget.dataset.id,
    //   'params.pageNum':1,
    //   'params.pageSize':20
    // })
    //   this.getProductList()
    console.log(e)
    let name = e.currentTarget.dataset.item.classifyName
    let id = e.currentTarget.dataset.item.id
    wx.navigateTo({
      url: '../proList/proList?name='+name+'&id='+id,
    })
  },
  initGlobalData:function(){
    let globalData=getApp().globalData;
    let statusBarHeightpx=globalData.statusBarHeightpx;
    let navHeadHeightpx=globalData.navHeadHeightpx;
    let navHeightpx=globalData.navHeightpx;
    this.setData({navHeadHeightpx:navHeadHeightpx,navHeightpx:navHeightpx,statusBarHeightpx:statusBarHeightpx});
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   this.loadCartNum()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.isFinshed)
      return

    let page = this.data.params.pageNum+1
    this.setData({
      'params.pageNum':page
    })
    if(this.tab_index == 0){
      this.getProductHot()
    }else{
      this.getProductList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})